package com.jinsi.controller.ylt;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.constant.YltConstant;
import com.jinsi.pojo.SyUserCouponUsed;
import com.jinsi.service.ylt.CouponService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @ClassName CouponController
 * @Content 优惠券Controller
 * @Author 姚龙腾
 * @Date 2022/10/12 10:40
 */
@RestController
@Slf4j
public class CouponController {

    @Autowired
    private CouponService couponService;


    /**
     * 查询看所有信息(模糊查询所需的信息)
     * @param page
     * @param limit
     * @param discountsName
     * @param discountsType
     * @param discountsQuantity
     * @return R
     */
    @RequestMapping("/getCouponList")
    public R getCouponList(Integer page, Integer limit, String discountsName, String discountsType, Integer discountsQuantity){
        PageHelper.startPage(page,limit);
        List<SyUserCouponUsed> list = couponService.getAll(discountsName, discountsType, discountsQuantity);
        PageInfo<SyUserCouponUsed> pageInfo = new PageInfo<>(list);
        return R.custom(YltConstant.LAYUI_CODE,
                YltConstant.LAYUI_MSG,
                pageInfo.getList(),
                pageInfo.getTotal());
    }


    /**
     * 添加优惠券
     * @param coupon
     * @return R
     */
    @RequestMapping("/addCoupon")
    public R addCoupon(SyUserCouponUsed coupon){
        try {
            couponService.addCoupon(coupon);
            log.info("添加优惠券 addCoupon方法已执行成功 添加的优惠劵 ${}" ,coupon);
            return R.ok();
        } catch (Exception e) {
            log.error("添加优惠券 addCoupon方法执行失败 报错原因 ${}" ,e);
            return R.error();
        }
    }



    /**
     * 修改上架还是下架
     * @param discountsId
     * @param discountsQuantity
     * @return R
     */
    @RequestMapping("/upCouponState")
    public R upCouponState(int discountsId, int discountsQuantity){
        try {
            couponService.upCouponState(discountsId,discountsQuantity);
            log.info("优惠券修改上架还是下架 upCouponState方法已执行成功 修改的优惠劵id ${}" ,discountsId);
            return R.ok();
        } catch (Exception e) {
            log.error("优惠券修改上架还是下架 upCouponState方法执行失败 报错原因 ${}" ,e);
            return R.error();
        }
    }


}
